package jupp;

import java.io.*;
import java.util.*;

public class JuppFileStore
{
	public static void main(String[] args) throws Exception
	{
		JuppMap<byte[]> jm = new JuppMap<byte[]>("jupp.file.store");
		byte[] content = jm.get("file");
		if (content != null)
		{
			System.out.println("Writing your file to tmp.tmp...");
			FileOutputStream fos = new FileOutputStream("tmp.tmp");
			fos.write(content);
			fos.close();
			System.out.println("Done. Specify a new file name:");
		}
		else
			System.out.println("Sorry, I don't have a file for you. Store which file?");
		Scanner scanner = new Scanner(System.in);
		String filename = scanner.nextLine();
		File f = new File(filename);
		FileInputStream fis = new FileInputStream(f);
		int sofar = 0;
		byte[] ab = new byte[(int)f.length()];
		while (true)
		{
			int got = fis.read(ab, sofar, ab.length - sofar);
			if (got < 0)
				break;
			sofar += got;
			if (sofar >= ab.length)
				break;
		}
		fis.close();
		jm.put("file", ab);
		System.out.println("OK, I'll write that file as 'tmp.tmp' next time. Bye.");
	}
}
